package com.kpmg.datalake.autopackage.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kpmg.datalake.autopackage.dao.IndustryTemplateManagerMapper;
import com.kpmg.datalake.autopackage.enums.TemplateStatusEnum;
import com.kpmg.datalake.autopackage.model.IndustryTemplateManager;
import com.kpmg.datalake.autopackage.service.IndustryTemplateService;
import com.kpmg.datalake.autopackage.utils.ExcelOperateUtil;
import com.kpmg.datalake.autopackage.vo.IndustryTemplateDTO;
import com.kpmg.datalake.common.vo.ServerResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import javax.annotation.Resource;

/**
 * @Author: Richard.ji [richard.ji@kpmg.com]
 * @Date: 2018-07-16 15:55:18
 * @Description:
 */
@Service
public class IndustryTemplateServiceImpl implements IndustryTemplateService {

    @Resource
    private IndustryTemplateManagerMapper industryTemplateManagerMapper;


    @Override
    public ServerResponse getIndustryTemplateList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, String>> sheetList;
    	
    	List<IndustryTemplateManager> industryList = industryTemplateManagerMapper.getIndustryTemplateList();
    	for(IndustryTemplateManager industryTemplate : industryList){
            String[] sheetNameArr;
            String[] sheetDescArr = {};
    	    if(!StringUtils.isEmpty(industryTemplate.getTotalSheet())){
    	        if(!StringUtils.isEmpty(industryTemplate.getSheetDesc())){
                    industryTemplate.setSheetList(industryTemplate.getSheetDesc());
                }else{
                    sheetNameArr = industryTemplate.getTotalSheet().split(",");
                    sheetList = ExcelOperateUtil.convertArrayToList(sheetNameArr, sheetDescArr);
                    industryTemplate.setSheetList(JSON.toJSONString(sheetList));
                }
            }

            industryTemplate.setUploadStatusStr(TemplateStatusEnum.getNameByCode(industryTemplate.getUploadStatus()));
        }

    	PageInfo<IndustryTemplateManager> requestPage = new PageInfo<>(industryList);
    	
        return ServerResponse.createBySuccess("获取行业模板列表成功", requestPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse insertIndustryTemplate(IndustryTemplateDTO industryTemplateDTO) {
        IndustryTemplateManager industryTemplate = new IndustryTemplateManager();

        BeanUtils.copyProperties(industryTemplateDTO, industryTemplate);

        int code = industryTemplateManagerMapper.insertSelective(industryTemplate);

        return ServerResponse.createBySuccess("行业模板保存成功", code);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse updateIndustryTemplate(IndustryTemplateDTO industryTemplateDTO) {
        IndustryTemplateManager industryTemplate = new IndustryTemplateManager();

        BeanUtils.copyProperties(industryTemplateDTO, industryTemplate);

        int code = industryTemplateManagerMapper.updateByPrimaryKeySelective(industryTemplate);

        return ServerResponse.createBySuccess("行业模板修改成功", code);
    }

    @Override
    public IndustryTemplateManager selectByPrimaryKey(String id) {
        return industryTemplateManagerMapper.selectByPrimaryKey(id);
    }

    @Override
    public ServerResponse updateByTplId(IndustryTemplateDTO industryTemplateDTO) {
        IndustryTemplateManager industryTemplate = new IndustryTemplateManager();

        BeanUtils.copyProperties(industryTemplateDTO, industryTemplate);

        int code = industryTemplateManagerMapper.updateByTplId(industryTemplate);

        return ServerResponse.createBySuccess("行业模板修改成功", code);
    }

    /**
     * 查询上传任务数
     * @return
     */
    @Override
    public Integer queryTaskCount() {
        return industryTemplateManagerMapper.queryTaskCount();
    }
}
